package com.bt7274.controller;

import com.bt7274.annotation.RequireLogin;
import com.bt7274.grace.result.GraceJSONResult;
import com.bt7274.pojo.bo.PromotionBO;
import com.bt7274.pojo.bo.PromotionUpdateBO;
import com.bt7274.pojo.bo.UserParticipateBO;
import com.bt7274.pojo.vo.ActivityRecordVO;
import com.bt7274.pojo.vo.PromotionListVO;
import com.bt7274.pojo.vo.PromotionVO;
import com.bt7274.pojo.vo.UserPromotionVO;
import com.bt7274.service.PromotionService;
import com.bt7274.utils.PageResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: bt7274-back
 * @BelongsPackage: com.bt7274.controller
 * @Author: Galland
 * @CreateTime: 2025-06-04
 * @Description: 促销活动管理
 * @Version: 1.0
 */

@Tag(name = "促销活动管理")
@RestController
@RequestMapping("/api/promotion")
@RequiredArgsConstructor
@Validated
public class PromotionController {
    private final PromotionService promotionService;

    // ==========管理员接口=======

    /**
     * 创建促销活动（管理员）
     *
     * @param promotionBO: 促销活动BO
     * @return GraceJSONResult
     * @author galland
     */
    @PostMapping("/admin/create")
    public GraceJSONResult createPromotion(@Valid @RequestBody PromotionBO promotionBO) {
        String promotionId = promotionService.createPromotion(promotionBO);
        return GraceJSONResult.ok(promotionId);
    }
    /**
     * 更新促销活动（管理员）
     *
     * @param promotionUpdateBO: 促销活动更新BO
     * @return GraceJSONResult
     * @author galland
     */
    @PutMapping("/admin/update")
    public GraceJSONResult updatePromotion(@Valid @RequestBody PromotionUpdateBO promotionUpdateBO) {
        promotionService.updatePromotion(promotionUpdateBO);
        return GraceJSONResult.ok("活动创建成功");
    }

    /**
     * 删除促销活动（管理员）
     *
     * @param id:
     * @return GraceJSONResult
     * @author galland
     */
    @DeleteMapping("/admin/delete/{id}")
    public GraceJSONResult deletePromotion(@PathVariable("id") String id) {
        promotionService.deletePromotion(id);
        return GraceJSONResult.ok("活动删除成功");
    }

    /**
     * 分页查询活动列表（管理员）
     *
     * @param current:
     * @param size:
     * @param type:
     * @param isActive:
     * @return GraceJSONResult
     * @author galland
     */
    @GetMapping("/admin/list")
    public GraceJSONResult getPromotionList(
        @RequestParam(defaultValue = "1") Integer current,
        @RequestParam(defaultValue = "10") Integer size,
        @RequestParam(required = false) String type,
        @RequestParam(required = false) Integer isActive) {

            PageResponse<PromotionListVO> result = promotionService.getPromotionList(current, size, type, isActive);
            return GraceJSONResult.ok(result);
    }

    /**
     * 启用/禁用活动（管理员）
     *
     * @param id:
     * @param isActive:
     * @return GraceJSONResult
     * @author galland
     */
    @PutMapping("/admin/toggle-status/{id}")
    public GraceJSONResult togglePromotionStatus(@PathVariable("id") String id,@RequestParam Integer isActive) {
        promotionService.togglePromotionStatus(id, isActive);
        String message = isActive == 1 ? "活动已启用" : "活动已禁用";
        return GraceJSONResult.ok(message);
    }

    /**
     * 获取活动参与记录（管理员）
     *
     * @param promotionId:
     * @param current:
     * @param size:
     * @return Result<PageResponse<ActivityRecordVO>>
     * @author galland
     */
    @Operation(summary = "获取活动参与记录")
    @GetMapping("/admin/activity-records/{promotionId}")
    public GraceJSONResult getActivityRecords(
            @PathVariable String promotionId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResponse<ActivityRecordVO> response = promotionService.getActivityRecords(promotionId, current, size);
        return GraceJSONResult.ok(response);
    }


    /**
     * 根据活动id获取活动详情
     *
     * @param id: 活动id
     * @return GraceJSONResult
     * @author galland
     */
    @GetMapping("/detail/{id}")
    public GraceJSONResult getPromotionDetail(@PathVariable("id") String id) {
        PromotionVO promotion = promotionService.getPromotionById(id);
        return GraceJSONResult.ok(promotion);
    }


    /**
     * 获取进行中的活动列表
     *
     * @param :
     * @return GraceJSONResult
     * @author galland
     */
    @GetMapping("/active")
    public GraceJSONResult getActivePromotion() {
        List<PromotionListVO> promotionListVO = promotionService.getActivePromotions();
        return GraceJSONResult.ok(promotionListVO);
    }

    /**
     * 获取用户可参加的活动
     *
     * @param userId:
     * @return Result<List<UserPromotionVO>>
     * @author galland
     */
    @Operation(summary = "获取用户可参加的活动")
    @GetMapping("/available")
    @RequireLogin
    public GraceJSONResult getAvailablePromotions(
            @RequestAttribute("userId") String userId) {
        List<UserPromotionVO> promotions = promotionService.getAvailablePromotions(userId);
        return GraceJSONResult.ok(promotions);
    }

    /**
     * 用户参加活动
     *
     * @param userId:
     * @param promotionId:
     * @param extraParams:
     * @return GraceJSONResult
     * @author galland
     */
    @Operation(summary = "参加活动")
    @PostMapping("/participate")
    @RequireLogin
    public GraceJSONResult participatePromotion(String userId, String promotionId, Map<String, Object> extraParams) {
        UserParticipateBO participateBO = new UserParticipateBO();
        participateBO.setUserId(userId);
        participateBO.setPromotionId(promotionId);
        participateBO.setExtraParams(extraParams);

        String reward = promotionService.participatePromotion(participateBO);
        return GraceJSONResult.ok(reward);
    }

    /**
     * 检查用户是否已经参加活动
     *
     * @param userId:
     * @param promotionId:
     * @return Result<Boolean>
     * @author galland
     */
    @Operation(summary = "检查是否已参加活动")
    @GetMapping("/check-participation")
    @RequireLogin
    public GraceJSONResult checkParticipation(
            @RequestAttribute("userId") String userId,
            @RequestParam String promotionId) {
        boolean hasParticipated = promotionService.hasParticipated(userId, promotionId);
        return GraceJSONResult.ok(hasParticipated);
    }


    /**
     * 获取用户的活动参与记录
     *
     * @param userId:
     * @param current:
     * @param size:
     * @return GraceJSONResult
     * @author galland
     */
    @Operation(summary = "获取用户的活动参与记录")
    @GetMapping("/my-records")
    @RequireLogin
    public GraceJSONResult getUserActivityRecords(
            @RequestAttribute("userId") String userId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResponse<ActivityRecordVO> response = promotionService.getUserActivityRecords(userId, current, size);
        return GraceJSONResult.ok(response);
    }

}
